package com.shcz.migu.widget.base;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.shcz.migu.utils.KeyboardUtils;


public class AutoHeightLayout extends ResizeLayout implements ResizeLayout.OnResizeListener {

    public static final int KEYBOARD_STATE_NONE = 100;
    public static final int KEYBOARD_STATE_FUNC = 102;
    public static final int KEYBOARD_STATE_BOTH = 103;

    protected Context mContext;
    protected int mAutoViewHeight;
    protected View mAutoHeightLayoutView;
    protected int mKeyboardState = KEYBOARD_STATE_NONE;
    private boolean isShowEmotionView = false;

    public AutoHeightLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mAutoViewHeight = KeyboardUtils.getDefKeyboardHeight(mContext);
    }

    public void enableEmotionKeyborad(){
        setOnResizeListener(this);
    }

    public int getmAutoViewHeight() {
        return mAutoViewHeight;
    }

    public int getmKeyboardState() {
        return mKeyboardState;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public void setAutoHeightLayoutView(View view) {
        mAutoHeightLayoutView = view;
    }

    public void setAutoViewHeight(final int height) {
        int heightDp = KeyboardUtils.px2dip(mContext, height);
        if (heightDp > 0 && heightDp != mAutoViewHeight) {
            mAutoViewHeight = heightDp;
            KeyboardUtils.setDefKeyboardHeight(mContext, mAutoViewHeight);
        }

        if (mAutoHeightLayoutView != null) {
            mAutoHeightLayoutView.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mAutoHeightLayoutView.getLayoutParams();
            params.height = height;
            mAutoHeightLayoutView.setLayoutParams(params);
        }
    }

    public void hideAutoView(){
        this.post(new Runnable() {
            @Override
            public void run() {
                KeyboardUtils.closeSoftKeyboard(mContext);
                setAutoViewHeight(0);
                if (mAutoHeightLayoutView != null) {
                    mAutoHeightLayoutView.setVisibility(View.GONE);
                }
            }
        });
        mKeyboardState = KEYBOARD_STATE_NONE ;
        if(keyBoardViewListener != null)
            keyBoardViewListener.onKeyBoardClose();
    }


    public void hideKeyboardView(){
        this.post(new Runnable() {
            @Override
            public void run() {
                if (mAutoHeightLayoutView != null) {
                    mAutoHeightLayoutView.setVisibility(View.GONE);
                }
            }
        });
        if(keyBoardViewListener != null)
            keyBoardViewListener.onKeyBoardClose();
    }

    public void showAutoView(){
        if (mAutoHeightLayoutView != null) {
            mAutoHeightLayoutView.setVisibility(VISIBLE);
            setAutoViewHeight(KeyboardUtils.dip2px(mContext, mAutoViewHeight));
        }
        mKeyboardState = mKeyboardState == KEYBOARD_STATE_NONE ? KEYBOARD_STATE_FUNC : KEYBOARD_STATE_BOTH ;
        isShowEmotionView = true;
    }

    @Override
    public void OnSoftPop(final int height) {
        mKeyboardState = KEYBOARD_STATE_BOTH;
        post(new Runnable() {
            @Override
            public void run() {
                setAutoViewHeight(height);
                if(mAutoHeightLayoutView != null)
                    mAutoHeightLayoutView.setVisibility(View.INVISIBLE);
            }
        });
        if(keyBoardViewListener != null)
            keyBoardViewListener.onSoftPop();
        isShowEmotionView = false;
    }

    @Override
    public void OnSoftClose(int height) {
        if(isShowEmotionView){
            mKeyboardState = mKeyboardState == KEYBOARD_STATE_BOTH ? KEYBOARD_STATE_FUNC : KEYBOARD_STATE_NONE ;
            if(keyBoardViewListener != null)
                keyBoardViewListener.onSoftClose();
        }else{
            hideKeyboardView();
            mKeyboardState = KEYBOARD_STATE_NONE;
        }
    }

    @Override
    public void OnSoftChanegHeight(final int height) {
        post(new Runnable() {
            @Override
            public void run() {
                setAutoViewHeight(height);
            }
        });
    }

    private KeyBoardViewListener keyBoardViewListener;

    public void setKeyBoardViewListener(KeyBoardViewListener keyBoardViewListener) {
        this.keyBoardViewListener = keyBoardViewListener;
    }

    public interface KeyBoardViewListener {
         void onSoftPop();
         void onSoftClose();
         void onKeyBoardClose();
    }

}